package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;

import java.awt.BorderLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JProgressBar;

import sys.DownloadProgressListener;
import sys.Video;
import sys.VideoListModel;

import org.jdesktop.swingx.JXGraph;
import javax.swing.JScrollPane;
import org.jdesktop.swingx.JXList;
import java.awt.Component;

public class JPanelDetails extends JPanel implements ListSelectionListener, DownloadProgressListener {
	private JTextField textFieldTitle;
	private JTextField textFieldUrl;
	private JTextField textFieldLocalPath;
	private final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
	private JPanel details;
	private JLabel lblProgress;
	private JProgressBar progressBar;

	private Video selectedVideo;
	private JGraphPanel graphPanel;
	private JXGraph graph;
	private JLabel lblSpeed;
	private JTextField textField;
	private JXList list;
	
	/**
	 * Create the panel.
	 */
	public JPanelDetails() {
		setBorder(new EmptyBorder(7, 7, 7, 5));
		setLayout(new BorderLayout(0, 0));
		tabbedPane.setBorder(null);
		add(tabbedPane);
		
		details = new JPanel();
		tabbedPane.addTab("Details", null, details, null);
		details.setLayout(new MigLayout("", "[][grow]", "[fill][fill][fill][fill][fill]"));
		
		lblProgress = new JLabel("Progress");
		details.add(lblProgress, "cell 0 1,alignx left");
		
		progressBar = new JProgressBar();
		details.add(progressBar, "cell 1 1,grow");
		
		JLabel lblTitle = new JLabel("Title:");
		details.add(lblTitle, "cell 0 0,alignx left,aligny center");
		
		textFieldTitle = new JTextField();
		lblTitle.setLabelFor(textFieldTitle);
		textFieldTitle.setEditable(false);
		textFieldTitle.setColumns(10);
		details.add(textFieldTitle, "cell 1 0,growx,aligny top");
		
		lblSpeed = new JLabel("Speed:");
		details.add(lblSpeed, "cell 0 2,alignx left");
		
		textField = new JTextField();
		textField.setEditable(false);
		details.add(textField, "cell 1 2,growx,aligny top");
		textField.setColumns(10);
		
		JLabel lblUrl = new JLabel("Url:");
		details.add(lblUrl, "cell 0 3,alignx left,aligny center");
		
		textFieldUrl = new JTextField();
		textFieldUrl.setEditable(false);
		details.add(textFieldUrl, "cell 1 3,growx,aligny top");
		textFieldUrl.setColumns(10);
		
		JLabel lblLocalPath = new JLabel("Local path:");
		details.add(lblLocalPath, "cell 0 4,alignx left,aligny center");
		
		textFieldLocalPath = new JTextField();
		textFieldLocalPath.setEditable(false);
		details.add(textFieldLocalPath, "cell 1 4,growx,aligny top");
		textFieldLocalPath.setColumns(10);
		
		graphPanel = new JGraphPanel();
		graphPanel.setBackground(UIManager.getColor("window"));
		graphPanel.setForeground(UIManager.getColor("Label.foreground"));
		tabbedPane.addTab("Speed", null, graphPanel, null);
		
		graph = new JXGraph();
		tabbedPane.addTab("Speed", null, graph, null);

	}

	private void selectionChanged() {
		selectedVideo.addDownloadProgressListener(this);
		textFieldTitle.setText(			selectedVideo.getTitle()			);
		textFieldUrl.setText(			selectedVideo.getUrl()				);
		textFieldLocalPath.setText(		selectedVideo.getDownloadedTo()		);
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		if(!e.getValueIsAdjusting() && !((DefaultListSelectionModel)e.getSource()).isSelectionEmpty()) {
			
			selectedVideo = (Video)JPanelMainContent.getVideoList().getSelectedValue();
			selectionChanged();
		}
	}

	@Override
	public void updateDownloadProgress(Video video) {
		progressBar.setValue(			selectedVideo.getDownloadProgress()	);
		graphPanel.setSpeeds(selectedVideo.getSpeeds());
		textField.setText(selectedVideo.getLastSpeed() + " Kb/s");
	}
}
